package qldc.pm.tiensu.portlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import qldc.pm.tiensu.action.BaoCaoThongKe;
import qldc.pm.tiensu.action.LapBienBanBanGiaoHoSo;
import qldc.pm.tiensu.action.QuanLyNguoiCoTienSu;
import qldc.pm.tiensu.action.QuanLyYeuCauTrichLuc;
import qldc.pm.tiensu.constants.TienSuConstants;
import qldc.pm.tiensu.portlet.action.ReportAction;
import qldc.pm.tiensu.portlet.action.ResourceRequestHandler;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class TienSuPortlet extends MVCPortlet {
	private static Log _log = LogFactoryUtil.getLog(TienSuPortlet.class);

	private static Map<String, Class<?>> actionHandlers;

	@Override
	public void init() throws PortletException {
		actionHandlers = new HashMap<String, Class<?>>();

		actionHandlers.put(TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU, QuanLyNguoiCoTienSu.class);

		actionHandlers.put(TienSuConstants.QUAN_LY_YEU_CAU_TRICH_LUC, QuanLyYeuCauTrichLuc.class);

		actionHandlers.put(TienSuConstants.BAO_CAO_THONG_KE, BaoCaoThongKe.class);
		actionHandlers.put(TienSuConstants.LAP_BIEN_BAN_BAN_GIAO_HO_SO, LapBienBanBanGiaoHoSo.class);

		actionHandlers.put(TienSuConstants.REPORT_ACTION, ReportAction.class);
		


		super.init();
	}

	private Map<String, Class<?>> getActionHandlers() {
		return actionHandlers;
	}

	/**
	 * the action request need to have at least 2 parameters: action handler and action name.
	 * action handler is the key of the action handlers map. With the key, we have the action class to handle the action request.
	 * action name is the name of the method that we want to invoke.
	 */
	@Override
	public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
		String actionHandlerParam = ParamUtil.getString(actionRequest, TienSuConstants.ACTION_HANDLER_PARAM);

		String action = ParamUtil.getString(actionRequest, Constants.ACTION);

		if(Validator.isNotNull(actionHandlerParam) && Validator.isNotNull(action)) {
			Class<?> actionHandler = getActionHandlers().get(actionHandlerParam);

			Method method;
			try {
				method = actionHandler.getMethod(action, ActionRequest.class, ActionResponse.class);

				method.invoke(actionHandler.newInstance(), actionRequest, actionResponse);
			} catch (Exception e) {
				_log.error(e);
			}
		}
	}

	/**
	 * All resource requests should be put in a specific class, named ResourceRequestHandler
	 * When receiving a request with the given id (resourceId), we will create a new instance of ResourceRequestHandler
	 * And invoke the method with the name is the same with the given id
	 */
	@Override
	public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {
		String resourceId = resourceRequest.getResourceID();

		if(Validator.isNotNull(resourceId)) {
			ResourceRequestHandler handler = new ResourceRequestHandler();

			Method method = null;
			try {
				method = ResourceRequestHandler.class.getMethod(resourceId, ResourceRequest.class, ResourceResponse.class);

				if(Validator.isNotNull(method)) {
					method.invoke(handler, resourceRequest, resourceResponse);
				}
			} catch (Exception e) {
				_log.error(e);
			}
		}
	}
}
